package com.pengcheng.taskapplication.mvp.view.fragment;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ScrollView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.pengcheng.taskapplication.R;
import com.pengcheng.taskapplication.adapter.LisenceAdapter;
import com.pengcheng.taskapplication.contants.ConstantValue;
import com.pengcheng.taskapplication.model.SubscribeOrderListModel;
import com.pengcheng.taskapplication.model.UserDetailVo;
import com.pengcheng.taskapplication.mvp.presenter.UserPresenter;
import com.pengcheng.taskapplication.mvp.view.activity.ApplySuccessActivity;
import com.pengcheng.taskapplication.mvp.view.activity.Html5AgressActivity;
import com.pengcheng.taskapplication.mvp.view.activity.LoginActivity;
import com.pengcheng.taskapplication.mvp.view.widget.MyListView;
import com.pengcheng.taskapplication.net.IView;
import com.pengcheng.taskapplication.rxbus.RxBusRoute;
import com.pengcheng.taskapplication.rxbus.RxEvent;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

public class MyApplyListFragment extends BaseFragment implements IView {

    @BindView(R.id.my_list)
    MyListView myListView;
    @BindView(R.id.pull_to_refresh_lv)
    PullToRefreshScrollView pullToRefreshListView;

    private int page = 1;
    private int size = 100;

    private UserPresenter userPresenter;
    ArrayList<SubscribeOrderListModel> listModelArrayList = new ArrayList<>();

    private LisenceAdapter adapter;

    @Override
    protected int layoutResID() {
        return R.layout.fragment_apply_list_layout;
    }

    @Override
    protected void initData() {
        userPresenter = new UserPresenter(this);
        adapter = new LisenceAdapter(context, listModelArrayList);
        myListView.setAdapter(adapter);
        myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                SubscribeOrderListModel model = (SubscribeOrderListModel) adapterView.getAdapter().getItem(i);
                Intent intent = new Intent(context, ApplySuccessActivity.class);
                intent.putExtra("code", model.getCode());
                startActivity(intent);
            }
        });
        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                page = 1;
                listModelArrayList.clear();
                getSubscribes(page, size);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                page++;
                getSubscribes(page, size);
            }
        });
        getSubscribes(page, size);
    }

    public void getSubscribes(int page, int pageSize) {
        userPresenter.getSubscribes(page, pageSize, ConstantValue.RequestKey.getSubscribes);
    }

    @Override
    public void onSuccess(String code, String msg, Object data, String actionType) {
        if (actionType.equals(ConstantValue.RequestKey.getSubscribes)) {
            listModelArrayList.addAll((ArrayList<SubscribeOrderListModel>) data);
            adapter.notifyDataSetChanged();
            pullToRefreshListView.onRefreshComplete();
        }
    }

    @Override
    public void onFailed(String code, String msg) {
        pullToRefreshListView.onRefreshComplete();
    }

    @OnClick({R.id.ll_blcp})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_blcp:
                Intent intent = new Intent(context, Html5AgressActivity.class);
                intent.putExtra("url", "file:///android_asset/instructions.html");
                intent.putExtra("title", "办理须知");
                startActivity(intent);
                break;
        }
    }

    @Override
    protected boolean isRegistRxBus() {
        return true;
    }

    /**
     * 注册订阅事件
     */
    protected void receiveEvent(RxEvent rxEvent) {
        switch (rxEvent.busName) {
            case RxBusRoute.REFRESH:
                page = 1;
                listModelArrayList.clear();
                getSubscribes(page, size);
                break;
        }
    }
}
